<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" style="width: 100%; margin-top: 20px" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" minWidth="300"> </el-table-column>
        <el-table-column prop="order_price" label="订单价格" minWidth="80px"> </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status==0">未付款</el-tag>
            <el-tag type="success" v-if="scope.row.pay_status==1">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{scope.row.add_time | DateFormat}}
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="300">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="open_editForm(scope.row.order_id)">
            </el-button>
            <!-- 查看物流信息 -->
            <el-button type="success" icon="el-icon-location" size="mini" @click="showprogress(scope.row.order_id)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :page-sizes="[1,  5, 10,20]" :page-size="5" layout="total,sizes, prev, pager, next" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>
    </el-card>
    <!-- 修改地址 -->
    <el-dialog title="修改地址" :visible.sync="editDialog" width="50%">
      <!-- 内容主体区域 -->
      <el-form label-width="100px" ref="addressFormref" :model="addressForm" :rules="addressFormRules"
        @close="resetAddForm('addressFormref')">
        <el-form-item label="订单编号" prop="order_number">
          <el-input autocomplete="off" v-model="addressForm.order_number" readonly></el-input>
        </el-form-item>
        <el-form-item label="支付状态" prop="pay_status">
          <template>
            <el-radio v-model="addressForm.pay_status" label="0">未付款</el-radio>
            <el-radio v-model="addressForm.pay_status" label="1">已付款</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="是否发货" prop="is_send">
          <template>
            <el-radio v-model="addressForm.is_send" label="否">未发货</el-radio>
            <el-radio v-model="addressForm.is_send" label="是">已发货</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="订单价格" prop="order_price">
          <el-input v-model="addressForm.order_price"></el-input>
        </el-form-item>
        <el-form-item label="订单支付方式" prop="order_pay">
          <el-select v-model="addressForm.order_pay" placeholder="请选择">
            <el-option value="0" label="未支付"></el-option>
            <el-option value="1" label="支付宝"></el-option>
            <el-option value="2" label="微信"></el-option>
            <el-option value="3" label="银行卡"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader v-model="addressForm.address1" :options="cityOptions">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input autocomplete="off" v-model="addressForm.address2"></el-input>
        </el-form-item>
        <el-form-item label="发票抬头" prop="order_fapiao_title">
          <el-input autocomplete="off" v-model="addressForm.order_fapiao_title"></el-input>
        </el-form-item>
        <el-form-item label="公司名称" prop="order_fapiao_company">
          <el-input autocomplete="off" v-model="addressForm.order_fapiao_company"></el-input>
        </el-form-item>
        <el-form-item label="发货地址" prop="consignee_addr">
          <el-input autocomplete="off" v-model="addressForm.consignee_addr"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit(addressForm.order_id)">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 查看物流信息 -->
    <el-dialog title="查看物流信息" :visible.sync="progressDialog" width="50%">
      <el-timeline :reverse="reverse">
        <el-timeline-item v-for="(activity, index) in progressList" :key="index" :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import cityOptions from '@/assets/js/citydata.js'
export default {
  data () {
    return {
      orderList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      editDialog: false,
      addressForm: {
        address1: [],
        address2: '',
        // 订单编号
        order_number: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: '',
        order_fapiao_company: '',
        consignee_addr: '',
        goods: [],
        order_price: 0,
        order_pay: 0
      },
      goodsList: [],
      addressFormRules: {
        address1: [{
          required: true,
          message: '请输入地址',
          trigger: 'blur'
        }],
        address2: [{
          required: true,
          message: '请输入地址',
          trigger: 'blur'
        }]
      },
      cityOptions,
      progressDialog: false,
      reverse: true,
      progressList: []
    }
  },
  created () {
    this.getOrderList()
  },
  computed: {},
  watch: {
    'queryInfo.query' (newVal, oldVal) {
      this.getOrderList()
      console.log(newVal)
    }
  },
  methods: {
    async getOrderList () {
      const {
        data: res
      } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 重置表单
    resetAddForm (formName) {
      this.$refs[formName].resetFields()
    },
    // 监听 页码值改变事件
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 修改地址
    async open_editForm (id) {
      const {
        data: res
      } = await this.$http.get(`orders/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.addressForm = res.data
      // 打开弹框
      this.editDialog = true
    },
    // 查看物流信息
    async showprogress (id) {
      this.progressList = [{
        time: '2018-05-10 09:39:00',
        ftime: '2018-05-10 09:39:00',
        context: '已签收,感谢使用顺丰,期待再次为您服务',
        location: ''
      },
      {
        time: '2018-05-10 08:23:00',
        ftime: '2018-05-10 08:23:00',
        context: '[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件',
        location: ''
      },
      {
        time: '2018-05-10 07:32:00',
        ftime: '2018-05-10 07:32:00',
        context: '快件到达 [北京海淀育新小区营业点]',
        location: ''
      },
      {
        time: '2018-05-10 02:03:00',
        ftime: '2018-05-10 02:03:00',
        context: '快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]',
        location: ''
      },
      {
        time: '2018-05-09 23:05:00',
        ftime: '2018-05-09 23:05:00',
        context: '快件到达 [北京顺义集散中心]',
        location: ''
      },
      {
        time: '2018-05-09 21:21:00',
        ftime: '2018-05-09 21:21:00',
        context: '快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]',
        location: ''
      },
      {
        time: '2018-05-09 13:07:00',
        ftime: '2018-05-09 13:07:00',
        context: '顺丰速运 已收取快件',
        location: ''
      },
      {
        time: '2018-05-09 12:25:03',
        ftime: '2018-05-09 12:25:03',
        context: '卖家发货',
        location: ''
      },
      {
        time: '2018-05-09 12:22:24',
        ftime: '2018-05-09 12:22:24',
        context: '您的订单将由HLA（北京海淀区清河中街店）门店安排发货。',
        location: ''
      },
      {
        time: '2018-05-08 21:36:04',
        ftime: '2018-05-08 21:36:04',
        context: '商品已经下单',
        location: ''
      }
      ]

      this.progressDialog = true
    },
    async submitEdit (id) {
      if (this.addressForm.is_send === '是') {
        this.addressForm.is_send = '1'
      } else {
        this.addressForm.is_send = '0'
      }
      const Form = JSON.parse(JSON.stringify(this.addressForm))
      Form.address1 = Form.address1.join(',')
      const {
        data: res
      } = await this.$http.put(`orders/${id}`, Form)
      if (res.meta.status !== 201) {
        return this.$message.error(res.meta.msg)
      }
      this.editDialog = false
      this.$message.success(res.meta.msg)
      this.getOrderList()
    }
  }
}

</script>
<style lang="less" scoped>
  .el-cascader {
    width: 100%;
  }

  .el-timeline {
    height: 500px;
    overflow: auto;
  }

  ul {
    list-style-type: none;
  }

  .goods-box {
    width: 90%;
    height: 300px;
    overflow: auto;
  }

  .el-select {
    width: 100%;
  }

</style>
